package product.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feign.utils.R;
import com.feign.valid.AddGroup;
import com.feign.valid.UpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import product.domain.ProductAttrValue;
import product.service.AttrService;
import org.springframework.web.bind.annotation.*;
import product.service.ProductAttrValueService;
import product.utils.GetUtils;
import product.vo.AttrRespVo;
import product.vo.AttrVo;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 商品属性(Attr)表控制层
 *
 * @author panghu
 * @since 2023-03-04 21:09:15
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {


    @Autowired
    private AttrService attrService;

    @Autowired
    private GetUtils getUtils;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 获取基础属性分页数据
     *
     * @param request
     * @return
     */
    @GetMapping("list/{catId}")
    public R getAttrListWithBase(HttpServletRequest request, @PathVariable Long catId) {
        Map<String, Object> params = getUtils.getParamsWithMapByRequest(request);
        Page<AttrRespVo> attrRespVoPagePage = attrService.queryPageWithAttrRespVo(params, catId);
        return R.ok().put("data", attrRespVoPagePage);
    }

    /**
     * 删除属性
     *
     * @param ids
     * @return
     */
    @PostMapping("remove")
    public R removeCategory(@RequestBody Long[] ids) {
        boolean result = attrService.removeBatchAttrByIds(List.of(ids));
        return result ? R.ok("删除属性成功") : R.error("删除属性失败");
    }


    /**
     * 增加属性
     *
     * @param attrVo
     * @return
     */
    @PostMapping("save")
    public R save(@Validated({AddGroup.class}) @RequestBody AttrVo attrVo) {
        boolean result = attrService.saveAttr(attrVo);
        return result ? R.ok("增加属性成功") : R.error("增加属性失败");
    }

    /**
     * 修改属性
     *
     * @param attrVo
     * @return
     */
    @PostMapping("update")
    public R edit(@Validated({UpdateGroup.class}) @RequestBody AttrVo attrVo) {
        boolean result = attrService.updateWithRelationById(attrVo);
        return result ? R.ok("修改属性成功") : R.error("修改属性失败");
    }


    /**
     * 获取单个属性的数据
     *
     * @param attrId
     * @return
     */
    @GetMapping("info/{attrId}")
    public R info(@PathVariable Long attrId) {
        AttrRespVo attrRespVo = attrService.getAttrVoById(attrId);
        return R.ok().put("data", attrRespVo);
    }

    @GetMapping("/base/listforspu/{spuId}")
    public R listForSpu(@PathVariable Long spuId) {
        List<ProductAttrValue> productAttrValues = productAttrValueService.baseAttrValueListForSpu(spuId);
        return R.ok().put("data", productAttrValues);
    }

    @PostMapping("update/{spuId}")
    public R update(@PathVariable Long spuId, @RequestBody List<ProductAttrValue> attrValues) {
        productAttrValueService.updateAttrBySkuId(spuId, attrValues);
        return R.ok("属性更新成功");
    }

}

